package com.kgar.util;

import cn.hutool.core.lang.UUID;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;


public class ReadDocUtil {

    public static String readDoc(InputStream is, String name) throws IOException {
        StringBuilder resullt = new StringBuilder();
        //首先判断文件中的是doc/docx
        try {
            if (name.endsWith(".doc")) {
                try(WordExtractor re = new WordExtractor(is)) {
                    resullt .append( re.getText());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (name.endsWith(".docx")) {
                XWPFDocument document = new XWPFDocument(is);
                try (POIXMLTextExtractor extractor = new XWPFWordExtractor(document)) {
                    resullt .append( extractor.getText());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (name.endsWith(".pdf")) {
                try (PDDocument document = PDDocument.load(is)) {

                    int pageSize = document.getNumberOfPages();
                    // 一页一页读取
                    for (int i = 0; i < pageSize; i++) {
                        // 文本内容
                        PDFTextStripper stripper = new PDFTextStripper();
                        // 设置按顺序输出
                        stripper.setSortByPosition(true);
                        stripper.setStartPage(i + 1);
                        stripper.setEndPage(i + 1);
                        String text = stripper.getText(document);
                        System.out.println(text.trim());
                        resullt.append(text.trim());
                        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-");

                        // 图片 内容
                        PDPage page = document.getPage(i);
                        PDResources resources = page.getResources();
                        Iterable<COSName> cosNames = resources.getXObjectNames();
                        if (cosNames != null) {
                            for (COSName cosName : cosNames) {
                                if (resources.isImageXObject(cosName)) {
                                    PDImageXObject Ipdmage = (PDImageXObject) resources.getXObject(cosName);
                                    BufferedImage image = Ipdmage.getImage();
                                    try (FileOutputStream out = new FileOutputStream("D:\\temp\\temp\\" + UUID.randomUUID() + ".png")) {
                                        ImageIO.write(image, "png", out);
                                    } catch (IOException ignored) {
                                    }
                                }
                            }
                        }
                    }

                } catch (IOException ignored) {
                }
            } else return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resullt.toString();
    }

}
